#include "common.h"

int main(int argc, char *argv[])
{
	int sockfd, portno;
	struct sockaddr_in dest;
	char buffer[MAXBUF];
	char buffer2[MAXBUF];
	//char* client_pseudo;
	//char fbuf[MAXBUF];
	fd_set  active_fd_set;
	// size_t bytes_written;

	/*---Make sure we have the right number of parameters---*/
	if (argc < 3) {
		fprintf(stderr, "usage %s hostname port\n", argv[0]);
		exit(0);
	}
	portno = atoi(argv[2]);

	/*---Create the socket---*/
	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if (sockfd < 0)
	{
		perror("ERROR opening socket");
		exit(1);
	}

	/*---Initialize server address/port struct---*/
	memset(&dest, 0, sizeof(dest));
	dest.sin_family      = AF_INET;
	dest.sin_addr.s_addr = htonl(INADDR_ANY);
	dest.sin_port        = htons(portno);

	/*---Connect to server---*/
	if (connect(sockfd, (struct sockaddr *) &dest, sizeof(dest)) < 0)
	{
		perror("ERROR on connecting");
		exit(1);
	}
	FD_ZERO (&active_fd_set);
	FD_SET(STDIN_FILENO, &active_fd_set);
	FD_SET (sockfd, &active_fd_set);

	printf("Say Hello!\n");

	memset(buffer,'\0',sizeof(buffer));
	memset(buffer2,'\0',sizeof(buffer2));

	if( Readline(STDIN_FILENO, buffer, MAXBUF-1) < 0)// Say Hello!
	{
		puts("Read failed");
		return 1;
	}

	char msg[] = "CLT";
	strcat(msg, buffer);
	msg[strlen(msg)-1] = '\n';
	msg[strlen(msg)] = '\0';

	if( Sendline(sockfd, msg, strlen(msg)) < 0) { // Send Hello to Server
		puts("Send failed");
		return 1;
	}
	if( Readline(sockfd, buffer, MAXBUF-1) < 0) // Read a menu of what you can do
	{
		puts("Read failed");
		return 1;
	}
	printf("%s", buffer); // Display the menu


	while (1){
		/* Specific work BY every client */
		/* Answer visitor or member */
		/* Send nickname and password*/
		/* Re-do until the client gets it right*/

		/* Get the menu for: */

		/* getting list of connected clients */
		/* creating a group of people && check whether it is unique */
		/* geting list of created groupes */
		/* joining an existing groupe */
		/* sending a message to groupe he belongs to  */
		/* banning a client  */


	}
	/*---Clean up---*/
	close(sockfd);

	return 0;
}

